package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.Notice;
import cn.fintecher.pangolin.service.manage.request.AddNoticeRequest;
import cn.fintecher.pangolin.service.manage.request.FindNoticeRequest;
import cn.fintecher.pangolin.service.manage.request.ModifyNoticeRequest;
import cn.fintecher.pangolin.service.manage.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: xiaqun
 * @Description: 公告相关接口
 * @Date: 10:00 2019/9/2
 */

@RestController
@RequestMapping("/api/noticeController")
@Api(value = "公告相关接口", description = "公告相关接口")
public class NoticeController extends BaseController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 多条件查询所有公告
     */
    @ApiOperation(value = "多条件查询所有公告", notes = "多条件查询所有公告")
    @GetMapping("/findAllNotice")
    public ResponseEntity<Page<Notice>> findAllNotice(FindNoticeRequest request, Pageable pageable) {
        log.info("REST request to find all notice,{}", request);
        Page<Notice> page = noticeService.findAllNotice(request, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 新增公告
     */
    @ApiOperation(value = "新增公告", notes = "新增公告")
    @PostMapping("/addNotice")
    public ResponseEntity<Void> addNotice(@Valid @RequestBody AddNoticeRequest request,
                                          @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add notice,{}", request);
        noticeService.addNotice(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除公告
     */
    @ApiOperation(value = "删除公告", notes = "删除公告")
    @DeleteMapping("/deleteNotice")
    public ResponseEntity<Void> deleteNotice(@RequestParam @ApiParam(value = "公告ID", required = true) String id) {
        log.info("REST request to delete notice,{}", id);
        noticeService.deleteNotice(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改公告
     */
    @ApiOperation(value = "修改公告", notes = "修改公告")
    @PutMapping("/modifyNotice")
    public ResponseEntity<Void> modifyNotice(@Valid @RequestBody ModifyNoticeRequest request,
                                             @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify notice,{}", request);
        noticeService.modifyNotice(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 首页公告列表查询
     */
    @ApiOperation(value = "首页公告列表查询", notes = "首页公告列表查询")
    @GetMapping("/getNoticeList")
    public ResponseEntity<List<Notice>> getNoticeList() {
        log.info("REST request to get notice list");
        List<Notice> list = noticeService.getNoticeList();
        return ResponseEntity.ok().body(list);
    }
}
